'use strict';

const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const { BrowserWindow, Notification } = require('electron')
const { app } = require('electron')
const path = require('path')
const CoreElectronWindow = require('ee-core/electron/window');

const winURL = !app.isPackaged
    ? `http://localhost:8080` // 开发路径
    : `file://${__dirname}/index.html` //生产路径

/**
 * example
 * @class
 */
class WindowsController extends Controller {

  constructor(ctx) {
    super(ctx);
  }


  async openWindow ({title, width= 400, height=300, url}) {
    const top = CoreElectronWindow.getMainWindow();
    const win = new BrowserWindow({
      parent: top,
      title: title,
      webPreferences: {
        //webSecurity: false,
        contextIsolation: false, // false -> 可在渲染进程中使用electron的api，true->需要bridge.js(contextBridge)
        nodeIntegration: true,
        //preload: path.join(appInfo.baseDir, 'preload', 'bridge.js'),
      },
    });
    win.setSize(width, height)
    win.setResizable(false)
    win.center()
    await win.loadURL(winURL + url)
    win.show()
    win.webContents.openDevTools()
    win.focus()
  }

  async openNotice () {
    // const top = CoreElectronWindow.getMainWindow();
    // const win = new BrowserWindow({
    //   parent: top,
    //   frame: false,
    //   webPreferences: {
    //     //webSecurity: false,
    //     contextIsolation: false, // false -> 可在渲染进程中使用electron的api，true->需要bridge.js(contextBridge)
    //     nodeIntegration: true,
    //     //preload: path.join(appInfo.baseDir, 'preload', 'bridge.js'),
    //   },
    // });

    const notification = new Notification({
      title: '有新的提货单',
      subtitle: '提货人：12345'
    })

    notification.show()
  }
}

WindowsController.toString = () => '[class WindowsController]';
module.exports = WindowsController;